

global projectdir ""
global datadir "$projectdir/data"


**********************************************************************************
**********************************************************************************

clear
gen pik = ""
cd $datadir/intermediate_files
save phf_interleave_b_2018_usiris_2018q4_LONG, replace

forvalues i = 21/136 {

	display in red "---------- QTIME: `i' of 136---------"

	cd $datadir/raw_pulls/lehd_usiris
	use phf_interleave_b_2018_usiris_2018q4, clear
	keep if flag_pikin_phf == 1
	
	keep pik sein spell_u2w seinunit1 e`i'
	drop if missing(e`i')
	rename e`i' wage
	gen qtime = `i'
	
	cd $datadir/intermediate_files
	append using phf_interleave_b_2018_usiris_2018q4_LONG
	save phf_interleave_b_2018_usiris_2018q4_LONG, replace
}

order pik sein spell_u2w qtime
sort pik sein spell_u2w qtime
compress
cd $datadir/intermediate_files
save phf_interleave_b_2018_usiris_2018q4_LONG, replace


clear
gen pik = ""
cd $datadir/intermediate_files
save opm_us_ehf_phf_2014_usiris_2018q4_LONG, replace

forvalues i = 2/124 {

	display in red "---------- QTIME: `i' of 136---------"

	cd $datadir/raw_pulls/lehd_usiris
	use opm_us_ehf_phf_2014_usiris_2018q4, clear
	keep if flag_pikin_opmehfphf == 1
	
	keep pik sein seinunit e`i'
	drop if missing(e`i')
	rename e`i' wage
	gen qtime = `i'
	
	cd $datadir/intermediate_files
	append using opm_us_ehf_phf_2014_usiris_2018q4_LONG
	save opm_us_ehf_phf_2014_usiris_2018q4_LONG, replace
}

order pik sein seinunit qtime
sort pik sein seinunit qtime
compress
cd $datadir/intermediate_files
save opm_us_ehf_phf_2014_usiris_2018q4_LONG, replace


**********************************************************************************
**********************************************************************************



**********************************************************************************
**********************************************************************************
cd $datadir/raw_pulls/lehd_usiris
use lehd_phf_interleave_b_usiris_2018q4, clear
keep if flag_pikin_phf == 1
* Observations are uniquely defined by a pik-sein-spell_u2w
keep pik sein spell_u2w seinunit1 e21-e132
rename seinunit1 seinunit

* reshape takes way too long...just loop by year and stack
*reshape long e, i(pik sein spell_u2w seinunit) j(quarter)

tempfile hold
save `hold', replace

clear
gen pik = ""
cd $datadir/intermediate_files
save organize_phf_usiris_2018q4, replace


set more off
forvalues i = 1990/2017 {

	display in red "`i'"
	
	use `hold', clear
	local q1 = 4*(`i' - 1985) + 1
	local q2 = 4*(`i' - 1985) + 2
	local q3 = 4*(`i' - 1985) + 3
	local q4 = 4*(`i' - 1985) + 4
	
	display in red "`q1', `q2', `q3' `q4'"
	keep pik sein spell_u2w seinunit e`q1' e`q2' e`q3' e`q4'
	rename e`q1' q1
	rename e`q2' q2
	rename e`q3' q3
	rename e`q4' q4
	
	keep if !missing(q1) | !missing(q2) | !missing(q3) | !missing(q4)
	
	gen year = `i'
	gen source = "Interleave"
	
	cd $datadir/intermediate_files
	append using organize_phf_usiris_2018q4
	save organize_phf_usiris_2018q4, replace
}


cd $datadir/raw_pulls/lehd_usiris
use opm_us_ehf_phf_2014_usiris_2018q4, clear
keep if flag_pikin_opmehfphf == 1
* Observations are uniquely defined by a pik-sein-seinunit
order e*, seq
keep pik sein seinunit e5-e124
order pik sein seinunit

tempfile hold
save `hold', replace

clear
gen pik = ""
cd $datadir/intermediate_files
save organize_opm_us_ehf_phf_2014_usiris_2018q4, replace

set more off
forvalues i = 1986/2015 {

	display in red "`i'"
	
	use `hold', clear
	local q1 = 4*(`i' - 1985) + 1
	local q2 = 4*(`i' - 1985) + 2
	local q3 = 4*(`i' - 1985) + 3
	local q4 = 4*(`i' - 1985) + 4
	
	display in red "`q1', `q2', `q3' `q4'"
	
	keep pik sein seinunit e`q1' e`q2' e`q3' e`q4'
	rename e`q1' q1
	rename e`q2' q2
	rename e`q3' q3
	rename e`q4' q4
	
	keep if !missing(q1) | !missing(q2) | !missing(q3) | !missing(q4)
	
	gen year = `i'
	gen source = "OPM"
	
	cd $datadir/intermediate_files
	append using organize_opm_us_ehf_phf_2014_usiris_2018q4
	save organize_opm_us_ehf_phf_2014_usiris_2018q4, replace
}

order pik year sein seinunit source spell_u2w
sort pik year sein seinunit source spell_u2w
compress
cd $datadir/intermediate_files
save organize_opm_us_ehf_phf_2014_usiris_2018q4, replace
**********************************************************************************
**********************************************************************************
